--- /dev/null
+unit gnugettextD5;\r
+// Information about this file:\r
+// $LastChangedDate: 2005-04-04 19:40:57 +0200 (Mon, 04 Apr 2005) $\r
+// $LastChangedRevision: 60 $\r
+// $HeadURL: svn://svn.berlios.de/dxgettext/trunk/dxgettext/sample/gnugettextD5.pas $\r
+\r
+// Delphi 5 optimized interface for gnugettext.pas\r
+// This unit must only be used on Delphi 5. When you upgrade to Delphi 6 or\r
+// later, you should remove this unit and replace all reference to gnugettextD5\r
+// with refernces to gnugettext.\r
+\r
+interface\r
+\r
+uses\r
+ Classes;\r
+ \r
+// Ansistring versions of the api\r
+function _(const szMsgId: string): string;\r
+function gettext(const szMsgId: string): string;\r
+function dgettext(const szDomain: string; const szMsgId: string): string;\r
+procedure TranslateComponent(AnObject: TComponent);\r
+\r
+\r
+\r
+//*****************************************************************************\r
+// Don't use anything in the interface below this line.\r
+// It only contains code or gnugettext.pas to make it compile with Delphi 5.\r
+\r
+type\r
+ UTF8String = AnsiString;\r
+\r
+const\r
+ PathDelim='\';\r
+ sLineBreak=#13#10;\r
+ \r
+function GetEnvironmentVariable(const VarName: string): string;\r
+function DirectoryExists(const Name:string):boolean;\r
+function IncludeTrailingPathDelimiter(s: string): string;\r
+function ExcludeTrailingPathDelimiter(s: string): string;\r
+procedure RaiseLastOSError;\r
+function StrToFloatDef(const S:String;Default:Extended):Extended;\r
+function Utf8Decode(const S: UTF8String): WideString;\r
+function Utf8Encode(const WS: WideString): UTF8String;\r
+\r
+\r
+\r
+implementation\r
+\r
+uses\r
+ filectrl, Windows, SysUtils,\r
+ gnugettext;\r
+\r
+function GetEnvironmentVariable(const VarName: string): string;\r
+var Size: Integer;\r
+begin\r
+ Size := Windows.GetEnvironmentVariable(PChar(VarName), nil, 0);\r
+ SetLength(Result, Size - 1);\r
+ Windows.GetEnvironmentVariable(PChar(VarName), PChar(Result), Size);\r
+end;\r
+\r
+function DirectoryExists(const Name:string):boolean;\r
+begin\r
+ Result := FileCtrl.DirectoryExists(Name);\r
+end;\r
+\r
+function IncludeTrailingPathDelimiter(s: string): string;\r
+begin\r
+ Result := IncludeTrailingBackslash(s);\r
+end;\r
+\r
+function ExcludeTrailingPathDelimiter(s: string): string;\r
+begin\r
+ Result := ExcludeTrailingBackslash(s);\r
+end;\r
+\r
+procedure RaiseLastOSError;\r
+begin\r
+ RaiseLastWin32Error;\r
+end;\r
+\r
+function StrToFloatDef(const S:String;Default:Extended):Extended;\r
+begin\r
+ if not TextToFloat(PChar(S), Result, fvExtended) then\r
+ Result := Default;\r
+end;\r
+\r
+function UnicodeToUtf8(Dest: PChar; MaxDestBytes: Cardinal; Source: PWideChar; SourceChars: Cardinal): Cardinal;\r
+var\r
+ i, count: Cardinal;\r
+ c: Cardinal;\r
+begin\r
+ Result := 0;\r
+ if Source = nil then\r
+ Exit;\r
+ count := 0;\r
+ i := 0;\r
+ if Dest <> nil then begin\r
+ while (i < SourceChars) and (count < MaxDestBytes) do begin\r
+ c := Cardinal(Source[i]);\r
+ Inc(i);\r
+ if c <= $7F then begin\r
+ Dest[count] := Char(c);\r
+ Inc(count);\r
+ end else\r
+ if c > $7FF then begin\r
+ if count + 3 > MaxDestBytes then\r
+ break;\r
+ Dest[count] := Char($E0 or (c shr 12));\r
+ Dest[count + 1] := Char($80 or ((c shr 6) and $3F));\r
+ Dest[count + 2] := Char($80 or (c and $3F));\r
+ Inc(count, 3);\r
+ end else // $7F < Source[i] <= $7FF\r
+ begin\r
+ if count + 2 > MaxDestBytes then\r
+ break;\r
+ Dest[count] := Char($C0 or (c shr 6));\r
+ Dest[count + 1] := Char($80 or (c and $3F));\r
+ Inc(count, 2);\r
+ end;\r
+ end;\r
+ if count >= MaxDestBytes then\r
+ count := MaxDestBytes - 1;\r
+ Dest[count] := #0;\r
+ end else begin\r
+ while i < SourceChars do begin\r
+ c := Integer(Source[i]);\r
+ Inc(i);\r
+ if c > $7F then begin\r
+ if c > $7FF then\r
+ Inc(count);\r
+ Inc(count);\r
+ end;\r
+ Inc(count);\r
+ end;\r
+ end;\r
+ Result := count + 1; // convert zero based index to byte count\r
+end;\r
+\r
+function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: Cardinal; Source: PChar; SourceBytes: Cardinal): Cardinal;\r
+var\r
+ i, count: Cardinal;\r
+ c: Byte;\r
+ wc: Cardinal;\r
+begin\r
+ if Source = nil then begin\r
+ Result := 0;\r
+ Exit;\r
+ end;\r
+ Result := Cardinal(-1);\r
+ count := 0;\r
+ i := 0;\r
+ if Dest <> nil then begin\r
+ while (i < SourceBytes) and (count < MaxDestChars) do begin\r
+ wc := Cardinal(Source[i]);\r
+ Inc(i);\r
+ if (wc and $80) <> 0 then begin\r
+ if i >= SourceBytes then\r
+ Exit; // incomplete multibyte char\r
+ wc := wc and $3F;\r
+ if (wc and $20) <> 0 then begin\r
+ c := Byte(Source[i]);\r
+ Inc(i);\r
+ if (c and $C0) <> $80 then\r
+ Exit; // malformed trail byte or out of range char\r
+ if i >= SourceBytes then\r
+ Exit; // incomplete multibyte char\r
+ wc := (wc shl 6) or (c and $3F);\r
+ end;\r
+ c := Byte(Source[i]);\r
+ Inc(i);\r
+ if (c and $C0) <> $80 then\r
+ Exit; // malformed trail byte\r
+\r
+ Dest[count] := WideChar((wc shl 6) or (c and $3F));\r
+ end else\r
+ Dest[count] := WideChar(wc);\r
+ Inc(count);\r
+ end;\r
+ if count >= MaxDestChars then\r
+ count := MaxDestChars - 1;\r
+ Dest[count] := #0;\r
+ end else begin\r
+ while (i < SourceBytes) do begin\r
+ c := Byte(Source[i]);\r
+ Inc(i);\r
+ if (c and $80) <> 0 then begin\r
+ if i >= SourceBytes then\r
+ Exit; // incomplete multibyte char\r
+ c := c and $3F;\r
+ if (c and $20) <> 0 then begin\r
+ c := Byte(Source[i]);\r
+ Inc(i);\r
+ if (c and $C0) <> $80 then\r
+ Exit; // malformed trail byte or out of range char\r
+ if i >= SourceBytes then\r
+ Exit; // incomplete multibyte char\r
+ end;\r
+ c := Byte(Source[i]);\r
+ Inc(i);\r
+ if (c and $C0) <> $80 then\r
+ Exit; // malformed trail byte\r
+ end;\r
+ Inc(count);\r
+ end;\r
+ end;\r
+ Result := count + 1;\r
+end;\r
+\r
+function Utf8Decode(const S: UTF8String): WideString;\r
+var\r
+ L: Integer;\r
+ Temp: WideString;\r
+begin\r
+ Result := '';\r
+ if S = '' then\r
+ Exit;\r
+ SetLength(Temp, Length(S));\r
+\r
+ L := Utf8ToUnicode(PWideChar(Temp), Length(Temp) + 1, PChar(S), Length(S));\r
+ if L > 0 then\r
+ SetLength(Temp, L - 1)\r
+ else\r
+ Temp := '';\r
+ Result := Temp;\r
+end;\r
+\r
+function Utf8Encode(const WS: WideString): UTF8String;\r
+var\r
+ L: Integer;\r
+ Temp: UTF8String;\r
+begin\r
+ Result := '';\r
+ if WS = '' then\r
+ Exit;\r
+ SetLength(Temp, Length(WS) * 3); // SetLength includes space for null terminator\r
+\r
+ L := UnicodeToUtf8(PChar(Temp), Length(Temp) + 1, PWideChar(WS), Length(WS));\r
+ if L > 0 then\r
+ SetLength(Temp, L - 1)\r
+ else\r
+ Temp := '';\r
+ Result := Temp;\r
+end;\r
+\r
+function _(const szMsgId: string): string;\r
+begin\r
+ Result:=gettext(szMsgid);\r
+end;\r
+\r
+function gettext(const szMsgId: string): string;\r
+begin\r
+ Result:=string(DefaultInstance.gettext(DefaultInstance.ansi2wideDTCP(szMsgId)));\r
+end;\r
+\r
+function dgettext(const szDomain: string; const szMsgId: string): string;\r
+begin\r
+ Result:=string(DefaultInstance.dgettext(szDomain,DefaultInstance.ansi2wideDTCP(szMsgId)));\r
+end;\r
+\r
+procedure TranslateComponent(AnObject: TComponent);\r
+begin\r
+ gnugettext.TranslateComponent(AnObject);\r
+end;\r
+\r
+end.\r